cces <- fread("raw_data/cces/CCES 2020/CES20_Common_OUTPUT_vv.csv") %>% 
  mutate(voted = is.finite(CL_2020gvm)) %>% 
  select(caseid,
         CC20_364a,
         CC20_364b,
         ideo = CC20_340a,
         pid7,
         birthyr,
         gender,
         educ,
         race,
         fox = CC20_300b_5,
         faminc_new,
         voted,
         commonpostweight,
         vvweight_post,
         party = CC20_433a,
         state = inputstate,
         pres_choice = CC20_410,
         CC20_410a_nv,
         presvote16post,
         starts_with("CC20_441")) %>% 
  mutate(fox = fox == 1,
         white = race == 1,
         party_n = party,
         party = ifelse(party == 1, "Democrat", ifelse(party == 2, "Republican", "Other")),
         race = ifelse(race == 1, "White",
                       ifelse(race == 2, "Black",
                              ifelse(race == 3, "Latinx",
                                     ifelse(race == 4, "Asian", "Other")))),
         age = 2020 - birthyr,
         pid7 = ifelse(pid7 == 1, "Strong Democrat",
                       ifelse(pid7 == 2, "Democrat",
                              ifelse(pid7 == 7, "Strong Republican",
                                     ifelse(pid7 == 6, "Republican",
                                            ifelse(pid7 == 3, "Lean Democrat",
                                                   ifelse(pid7 == 5, "Lean Republican",
                                                          ifelse(pid7 == 4, "Neither", "Other"))))))),
         voted_16 = presvote16post != 7,
         white = race == "White",
         CC20_441a = 6 - CC20_441a,
         rr = (CC20_441a + CC20_441b) / 2) %>% 
  filter(white) %>% 
  mutate_at(vars(race, state, party, pres_choice), as.factor) |> 
  mutate(pres_choice = ifelse(is.na(pres_choice), CC20_410a_nv, pres_choice),
         college = educ >= 5,
         pre_pref = ifelse(is.na(CC20_364a), CC20_364b, CC20_364a))

cces$pid7 <- factor(cces$pid7,
                    levels = c("Strong Democrat", "Democrat", "Lean Democrat", "Neither",
                               "Lean Republican", "Republican", "Strong Republican", "Other"))
income_lu <- data.frame("faminc_new" = c(1:16),
                        "income" = c(5000, 15000, 25000, 35000, 45000, 55000, 65000, 75000,
                                     90000, 110000, 135000, 175000, 225000, 300000, 425000, 500000))

cces <- left_join(cces, income_lu)

#########################################

modules <- readRDS("temp/ces_confidence.rds")

cces <- inner_join(cces, modules) %>% 
  mutate(caseid = as.factor(caseid))

full_samp <- cces

cces <- filter(cces, !is.na(post))

cces <- bind_rows(
  cces %>% 
    select(-pre) %>% 
    rename(conf = post) %>% 
    mutate(post = 1),
  cces %>% 
    select(-post) %>% 
    rename(conf = pre) %>% 
    mutate(post = 0)
)

cces$pres_choice <- as.factor(cces$pres_choice)

cces$trump <- as.factor(cces$pres_choice == 2)

cces$team <- as.factor(cces$team)

cces$fox <- as.integer(ifelse(is.na(cces$fox), F, cces$fox))

full <- cces

cces <- cces[complete.cases(select(cces, post, rr, ideo, caseid, team, trump, teamweight, fox,
                                   gender, college, income, age, pres_choice)),
             c("conf", "post", "rr", "ideo", "caseid", "team", "trump", "teamweight",
               "fox", "gender", "college", "income", "age", "pres_choice", "pid7")]

cces$caseid <- as.factor(paste0("id", cces$caseid))

cces$fox <- as.logical(cces$fox)

cces$female <- as.logical(cces$gender == 2)

cces <- cces |> 
  mutate(across(c(college, post, fox, female, trump), as.numeric))

cces_t <- filter(cces, pres_choice == 2)

cces_nt <- filter(cces, pres_choice == 1)

dats <- list(cces, cces_t, cces_nt)

models_full <- lapply(c(1:length(dats)), function(df){
  
  dat <- dats[[df]]
  
  
  if(df == 1){
    m1 <- feols(conf ~ post * rr + post * ideo + trump * post + fox * post +
                  post * income + post * gender + post * college + post * age +
                  team * post | caseid, dat, weights = ~ teamweight, cluster = c("caseid", "team"))
    
    m2 <- feols(conf ~ post * rr + post * ideo + trump * post + fox * post +
                  post * income + post * gender + post * college + post * age +
                  team * post, dat, weights = ~ teamweight, cluster = c("caseid", "team"))
    
    m3 <- feols(conf ~ rr + ideo + trump + fox +
                  income + gender + college + age |
                  team, filter(dat, post == 1), weights = ~ teamweight, cluster = c("team"))
    
    m4 <- feols(conf ~ rr + ideo + trump + fox +
                  income + gender + college + age |
                  team, filter(dat, post == 0), weights = ~ teamweight, cluster = c("team"))
    
    m5 <- feols(conf ~ post * rr + post * ideo + trump * post + fox * post +
                  post * income + post * gender + post * college + post * age +
                  team * post | caseid, dat, weights = ~ teamweight, cluster = c("caseid", "team"))
  }else{
    m1 <- feols(conf ~ post * rr + post * ideo + fox * post +
                  post * income + post * gender + post * college + post * age +
                  team * post | caseid, dat, weights = ~ teamweight, cluster = c("caseid", "team"))
    
    m2 <- feols(conf ~ post * rr + post * ideo + fox * post +
                  post * income + post * gender + post * college + post * age +
                  team * post, dat, weights = ~ teamweight, cluster = c("caseid", "team"))
    
    m3 <- feols(conf ~ rr + ideo + fox +
                  income + gender + college + age |
                  team, filter(dat, post == 1), weights = ~ teamweight, cluster = c("team"))
    
    m4 <- feols(conf ~ rr + ideo + fox +
                  income + gender + college + age |
                  team, filter(dat, post == 0), weights = ~ teamweight, cluster = c("team"))
    
    m5 <- feols(conf ~ post * rr + post * ideo + fox * post +
                  post * income + post * gender + post * college + post * age +
                  post * pid7 +
                  team * post | caseid, dat, weights = ~ teamweight, cluster = c("caseid", "team"))
  }
  
  assign("dat", dat, envir = .GlobalEnv)  ## have to do this for the marginal effects plot to run
  
  d1 <- plot_model(m2, type = "eff", terms = c("rr", "post"))[["data"]] |> 
    mutate(group = ifelse(group == 1, "Post-Election", "Pre-Election"),
           m = case_when(df == 1 ~ "All Respondents",
                         df == 2 ~ "Trump Supporters",
                         df == 3 ~ "Biden Supporters")) |> 
    select(x, predicted, conf.low, conf.high, group, m)
  rm("dat", envir = .GlobalEnv) ## cleanup
  return(list(m1, m2, d1, m3, m4, m5))
})


rows <- tribble(~term, ~m1,  ~m2, ~m3,
                "Respondent Fixed Effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$",
                "Team Fixed Effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$",
                "Post × Team Fixed Effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$")

attr(rows, 'position') <- c(19:21)

modelsummary(list(models_full[[1]][[1]], models_full[[2]][[1]], models_full[[3]][[1]]),
             stars = c("*" = 0.05, "**" = 0.01, "***" = 0.001),
             coef_map = c("post:rr" = "Post × Racial Resentment",
                          "post:income" = "Post × Family Income",
                          "post:gender" = "Post × Female",
                          "post:college" = "Post × 4-year Degree",
                          "post:age" = "Post × Age",
                          "post:ideo" = "Post × Ideology",
                          "post:trump" = "Post × Supported Trump",
                          "post:fox" = "Post × Watched Fox News",
                          "post" = "Post"),
             gof_omit = 'DF|Deviance|AIC|BIC|Within|Pseudo|Log|Std|FE|RMSE',
             add_rows = rows,
             title = "\\label{tab:ces-regs} Confidence in Election Results, 2020 CES",
             notes = list("Robust standard errors clustered by respondent and team."),
             output = "latex",
             escape = FALSE,
             col.names = NULL) |> 
  kableExtra::add_header_above(c("", "Respondents", "Supporters", "Supporters"), line = T) |> 
  kableExtra::add_header_above(c("", "All", "Trump", "Biden"), line = F) |> 
  kableExtra::save_kable("Output/tables/ces_regs.tex")

rows <- tribble(~term, ~m1,  ~m2, ~m3,
                "Respondent Fixed Effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$",
                "Post × 7pt PID", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$",
                "Team Fixed Effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$",
                "Post × Team Fixed Effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$")

attr(rows, 'position') <- c(19:22)

modelsummary(list(models_full[[1]][[6]], models_full[[2]][[6]], models_full[[3]][[6]]),
             stars = c("*" = 0.05, "**" = 0.01, "***" = 0.001),
             coef_map = c("post:rr" = "Post × Racial Resentment",
                          "post:income" = "Post × Family Income",
                          "post:gender" = "Post × Female",
                          "post:college" = "Post × 4-year Degree",
                          "post:age" = "Post × Age",
                          "post:ideo" = "Post × Ideology",
                          "post:trump" = "Post × Supported Trump",
                          "post:fox" = "Post × Watched Fox News",
                          "post" = "Post"),
             gof_omit = 'DF|Deviance|AIC|BIC|Within|Pseudo|Log|Std|FE|RMSE',
             add_rows = rows,
             title = "\\label{tab:ces-regs-pid} Confidence in Election Results, 2020 CES",
             notes = list("Robust standard errors clustered by respondent and team."),
             output = "latex",
             escape = FALSE,
             col.names = NULL) |> 
  kableExtra::add_header_above(c("", "Respondents", "Supporters", "Supporters"), line = T) |> 
  kableExtra::add_header_above(c("", "All", "Trump", "Biden"), line = F) |> 
  kableExtra::save_kable("Output/tables/ces_regs_pid.tex")

rows <- tribble(~term, ~m1,  ~m2, ~m3,
                "Team Fixed Effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$")

attr(rows, 'position') <- c(17:17)

modelsummary(list(models_full[[1]][[4]], models_full[[2]][[4]], models_full[[3]][[4]]),
             stars = c("*" = 0.05, "**" = 0.01, "***" = 0.001),
             coef_map = c("rr" = "Racial Resentment",
                          "income" = "Family Income",
                          "gender" = "Female",
                          "college" = "4-year Degree",
                          "age" = "Age",
                          "ideo" = "Ideology",
                          "trump" = "Supported Trump",
                          "fox" = "Watched Fox News"),
             gof_omit = 'DF|Deviance|AIC|BIC|Within|Pseudo|Log|Std|FE|RMSE',
             add_rows = rows,
             title = "\\label{tab:ces-regs-post} Confidence in Election Results, 2020 CES (Post-Election)",
             notes = list("Robust standard errors clustered by team.",
                          "``Intercept'' term subsumed by team fixed effects."),
             output = "latex",
             escape = FALSE,
             col.names = NULL) |> 
  kableExtra::add_header_above(c("", "Respondents", "Supporters", "Supporters"), line = T) |> 
  kableExtra::add_header_above(c("", "All", "Trump", "Biden"), line = F) |> 
  kableExtra::save_kable("Output/tables/ces_regs_post_only.tex")

modelsummary(list(models_full[[1]][[5]], models_full[[2]][[5]], models_full[[3]][[5]]),
             stars = c("*" = 0.05, "**" = 0.01, "***" = 0.001),
             coef_map = c("rr" = "Racial Resentment",
                          "income" = "Family Income",
                          "gender" = "Female",
                          "college" = "4-year Degree",
                          "age" = "Age",
                          "ideo" = "Ideology",
                          "trump" = "Supported Trump",
                          "fox" = "Watched Fox News",
                          "(Intercept)" = "Intercept"),
             gof_omit = 'DF|Deviance|AIC|BIC|Within|Pseudo|Log|Std|FE|RMSE',
             add_rows = rows,
             title = "\\label{tab:ces-regs-pre} Confidence in Election Results, 2020 CES (Pre-Election)",
             notes = list("Robust standard errors clustered by team.",
                          "``Intercept'' term subsumed by team fixed effects."),
             output = "latex",
             escape = FALSE,
             col.names = NULL) |> 
  kableExtra::add_header_above(c("", "Respondents", "Supporters", "Supporters"), line = T) |> 
  kableExtra::add_header_above(c("", "All", "Trump", "Biden"), line = F) |> 
  kableExtra::save_kable("Output/tables/ces_regs_pre_only.tex")



####

marg <- rbindlist(lapply(models_full, function(x) x[[3]]))

marg$m <- factor(marg$m, levels = c("All Respondents", "Trump Supporters", "Biden Supporters", "Fox Viewers"))
marg$group <- factor(marg$group, levels = c("Pre-Election", "Post-Election"))


p <- ggplot(filter(marg), aes(x = x, y = predicted, linetype = group,
                              ymin = conf.low, ymax = conf.high,
                              shape = group)) +
  facet_grid(. ~ m) +
  geom_point() +
  geom_errorbar(width = 0.1) + 
  geom_line() +
  theme_bc() +
  scale_x_continuous(breaks = c(1, 5), labels = c("Low", "High")) +
  scale_linetype_manual(values = c("dashed", "solid")) +
  labs(x = "Racial Resentment", y = "Confidence in Election",
       linetype = "Period",
       shape = "Period")
p
ggsave("Output/figures/ces_marg.png", width = 6, height = 4, units = "in")
fwrite(marg, "temp/marginal_effects_data_ces.csv")


########################################
########################################
########################################
########################################

full_samp$pres_choice <- ifelse(full_samp$pre_pref == 1, "Trump",
                                ifelse(full_samp$pre_pref == 2, "Biden", "Other"))

full_samp$t <- full_samp$pres_choice == "Trump"

full_samp$team <- as.factor(full_samp$team)

full_samp$fox <- as.integer(ifelse(is.na(full_samp$fox), F, full_samp$fox))

full_samp$missing <- is.na(full_samp$post)

mis_mod <- feols(missing ~ pre + ideo + fox  + t +
                   income + gender + college + age | team, filter(full_samp), weights = ~teamweight)

mis_mod2 <- feols(missing ~ pre + ideo + fox  +
                   income + gender + college + age | team, filter(full_samp, pres_choice == "Trump"), weights = ~teamweight)

mis_mod3 <- feols(missing ~ pre + ideo + fox  +
                   income + gender + college + age | team, filter(full_samp, pres_choice == "Biden"), weights = ~teamweight)


rows <- tribble(~term, ~m1, ~m2, ~m3,
                "Team Fixed Effects", "$\\checkmark$",  "$\\checkmark$",  "$\\checkmark$")

attr(rows, 'position') <- c(15:15)

modelsummary(list(mis_mod, mis_mod2, mis_mod3),
             stars = c("*" = 0.05, "**" = 0.01, "***" = 0.001),
             coef_map = c("pre" = "Pre-Election Confidence",
                          "income" = "Family Income",
                          "gender" = "Female",
                          "college" = "4-year Degree",
                          "age" = "Age",
                          "ideo" = "Ideology",
                          "tTRUE" = "Supported Trump (Pre-Election)",
                          "fox" = "Watched Fox News"),
             gof_omit = 'DF|Deviance|AIC|BIC|Within|Pseudo|Log|Std|FE|RMSE',
             # vcov = list(~caseid, ~caseid, ~caseid),
             add_rows = rows,
             title = "\\label{tab:ces-missing-reg} Attrition from Pre- to Post-Election Wave, 2020 CES",
             notes = list("Robust standard errors clustered by team."),
             output = "latex",
             escape = FALSE,
             col.names = NULL) |> 
  kableExtra::add_header_above(c("", "Respondents" = 1, "Supporters" = 1, "Supporters" = 1), line = T) |> 
  kableExtra::add_header_above(c("", "All" = 1, "Trump" = 1, "Biden" = 1), line = F) |> 
  kableExtra::save_kable("Output/tables/ces_attrition_reg.tex")
